package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelService;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/hotel")
public class HotelController {

    @Autowired
    private IHotelService hotelService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo<Hotel> result = hotelService.query(qo);
        model.addAttribute("result",result);
        return "hotel/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Hotel entity, MultipartFile img){
        //判断有没有设置封
        if (img != null && img.getSize() > 0 && img.getContentType().startsWith("image")){
            //把图片上传到本地图库,返回图片路径
            String url = UploadUtil.upload(img);
            //替换以前默认的封面
            entity.setCoverUrl(url);
        }
        hotelService.saveOrUpdate(entity);
        return "redirect:/hotel/list.do";
    }
}
